using System;
using System.Collections.Generic;
using System.Text;
using DCI6.Client.API;
using DCI6;

namespace DCIAPIRMSampleCode {
    internal class DDSConfigurationSampleCode {
        /// <summary>
        /// Sample Code which creates DDS Configuration
        /// properties and registers them with the DCI Service.
        /// </summary>
        public DDSConfiguration CreateConfiguration() {
            /**
       * Configure the DDS Region. Substitute your
       * specific DDS Connection properties.
       */
            DDSConfiguration config = new DDSConfiguration();

            if (System.IO.File.Exists("configurations.xml")) {
                config.FromXml(System.IO.File.ReadAllText("configurations.xml"));
            } else {
                config.ConnectionID = "TestRegion";
                config.DdsContext = "/DDS/services/DDS";
                config.DdsHost = "10.12.81.204";
                config.DdsPassword = "fsadmin";
                config.DdsPort = "7443";
                config.DdsProtocol = "https";
                config.DdsUser = "ddsuser";
                config.DdsSubscriberHost = "172.16.200.62";
                config.UseTS3Security = false;
                config.WSDL_TIMEOUT = 5000;
                config.SOCKET_TIMEOUT = 5000;
                // config.ItemDataSchemaVersioning = DDSConfiguration.ItemDataSchemaVersion.SOFTWARE_BLOCK_11_12;
                config.ItemDataSchemaVersioning = DDSConfiguration.ItemDataSchemaVersion.SOFTWARE_BLOCK_13_14;
            }
            return config;
        }

        public void RegisterDDSConfiguration(DDSConfiguration config) {
            /**
       * Registers the DDS Connection Properties with the DCI Service.
       * After successfull registration use the value in this 
       * object's ConnectionID property as a parameter to api calls 
       * which make requests to DDS.
       */
            Console.WriteLine("Configuring DDS ID: " + config.ConnectionID);
            config.ConfigureDDSConfiguration();
        }


        public void DeleteConfiguration(DDSConfiguration configToDelete) {
           /**
           * Calling deleteDDSConfig will remove the DDS Configuration
           * properties associated with this instances Connection ID.
           * Any attempt to reference this connection ID after calling delete 
           * will result in an Exception
           */
            Console.WriteLine("Deleting DDS ID: " + configToDelete.ConnectionID);
            configToDelete.DeleteDDSConfiguration();
        }

        public void QueryDDSConfigurations() {
            /**
             * Calling getDDSConfigurations will return a Collection
             * of all DDSConfiguration Instances that are currently 
             * configured within the DCI Service
            */
            Console.WriteLine("Getting DDS Configurations...");
            List<DDSConfiguration> configs = DDSConfiguration.GetDDSConfigurations();
            Console.WriteLine("DCI Returned [{0}] DDS Configurations:", configs.Count);

            foreach (DDSConfiguration c in configs) {
                Console.WriteLine("DDS Region COnnection ID: " + c.ConnectionID);
            }
        }


        public void UpdateConfiguration(DDSConfiguration configWithSameConnID) {
            /**
	       * To update a DDS Configuration you need to delete the existing configuration
	       * on the DCI Service.
	       * Calling ConfigureDDSConfig with an instance of DDSConfiguration whose
	       * ConnectionID has already been configured to the DCI Service will result
	       * in a DDSConfigurationException.
	       */
            Console.WriteLine("Attempting to configure DDS Configuration: " + configWithSameConnID.ConnectionID);

            List<DDSConfiguration> configs = new List<DDSConfiguration>();

            //Sample of Exception		  
            try {
                configWithSameConnID.ConfigureDDSConfiguration();
            } catch (DDSConfigurationException e) {
                Console.WriteLine(e.Message);
                configs = configWithSameConnID.GetDDSConfigurationsFromException(e);
                foreach (DDSConfiguration d in configs) {
                    Console.WriteLine("DDS Configuration: " + d.ConnectionID);
                }
            } catch (DCIConnectionException e) {
                Console.WriteLine(e.Message);
            } catch (DCIException e) {
                Console.WriteLine(e.Message);
            }

            try {
                //Remove Configuration if it exists
                foreach (DDSConfiguration d in configs) {
                    if (d.ConnectionID == configWithSameConnID.ConnectionID) {
                        d.DeleteDDSConfiguration();
                    }
                }
            } catch (DCIConnectionException e) {
                Console.WriteLine(e.Message);
            } catch (DCIException e) {
                Console.WriteLine(e.Message);
            }

            //Configure Updated DDS
            try {
                configWithSameConnID.ConfigureDDSConfiguration();
            } catch (DDSConfigurationException e) {
                Console.WriteLine(e.Message);
            } catch (DCIConnectionException e) {
                Console.WriteLine(e.Message);
            } catch (DCIException e) {
                Console.WriteLine(e.Message);
            }
        }
    }
}